from mcp.server import FastMCP
from config import logger
from mcp_tools import (
    connect_database,
    get_database_info,
    execute_sql,
    natural_language_query,
)

mcp = FastMCP("mcp_mysql")


@mcp.tool()
async def connect_database_tool(
    host: str = "localhost",
    port: int = 3306,
    user: str = "root",
    password: str = "root",
    database: str = "school",
) -> str:
    return await connect_database(host, port, user, password, database)


@mcp.tool()
async def get_database_info_tool() -> str:
    return await get_database_info()


@mcp.tool()
async def execute_sql_tool(sql: str) -> str:
    return await execute_sql(sql)


@mcp.tool()
async def natural_language_query_tool(query: str) -> str:
    return await natural_language_query(query)


if __name__ == "__main__":
    try:
        logger.info(f"MCP MYSQL 服务器正在启动")
        mcp.run()
    except KeyboardInterrupt:
        logger.info(f"服务已经停止")
    except Exception as e:
        logger.error(f"服务器运行错误:{str(e)}")
        import traceback

        # 打印异步的详细 的堆栈信息
        traceback.print_exc()
